package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


@RestController
public class EmpController {

    @Autowired
    private EmpService empService;
    @GetMapping("/emps")
    public Result selectAllEmp(@RequestParam(defaultValue = "1") Integer page ,
                               @RequestParam(defaultValue = "10")Integer pageSize,
                               String name,
                               Integer gender,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                               @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end
    ){
        PageBean pageBean = empService.selectAllEmp(page , pageSize , name ,gender , begin ,end);

        return Result.success(pageBean);
    }

    @DeleteMapping("/emps/{ids}")
    public Result deleteEmpByIds(@PathVariable List<Integer> ids){
        empService.deleteEmpByIds(ids);
        return Result.success();
    }

    @PostMapping("/emps")
    public Result insertEmp(@RequestBody Emp emp){

        empService.insertEmp(emp);
        return Result.success();
    }


    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/emps/{id}")
    public Result selectEmpById(@PathVariable Integer id){
        Emp emp = empService.selectEmpById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result updateEmpInfo(@RequestBody Emp emp){
        empService.updateEmpInfo(emp);
        return Result.success();
    }

}
















